package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.IncomingInspectionOrderMapper;
import com.erp.programmer.domain.*;
import com.erp.programmer.dto.IncomingInspectionDetailsOrderDto;
import com.erp.programmer.dto.IncomingInspectionOrderDto;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.enums.InspectStateEnum;
import com.erp.programmer.service.*;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-05-24
 */
@Service
@Transactional
public class IncomingInspectionOrderServiceImpl extends ServiceImpl<IncomingInspectionOrderMapper, IncomingInspectionOrder> implements IncomingInspectionOrderService {

    @Autowired
    private ProductService productService;
    @Autowired
    private IncomingInspectionDetailsOrderService incomingInspectionDetailsOrderService;
    @Autowired
    private IPurchaseOrderDetailService iPurchaseOrderDetailService;
    @Autowired
    private StockOrderService stockOrderService;
    @Autowired
    private IPurchaseOrderService iPurchaseOrderService;

    /**
     * 生成出库验证单
     * @param incomingInspectionOrderDto
     * @return
     */
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> add(IncomingInspectionOrderDto incomingInspectionOrderDto){

        //获取采购单id
        String purchaseId = incomingInspectionOrderDto.getPurchaseId();
        //验证该采购单id是否已经生成进库验货单
        QueryWrapper<IncomingInspectionOrder> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("purchase_id",purchaseId);
        if(getOne(queryWrapper1) != null){
            return ResponseDto.errorByMsg(CodeMsg.INCOMING_ORDER_NOT_EXIST);
        }
        //获取全部采购单详情
        QueryWrapper<PurchaseOrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_id",purchaseId);
        List<PurchaseOrderDetail> purchaseOrderDetailsDb = iPurchaseOrderDetailService.list(queryWrapper);

        IncomingInspectionOrder incomingInspectionOrder = new IncomingInspectionOrder();
        //进库验货单id
        String incomingInspectionId = String.valueOf(IdWorker.getId());
        incomingInspectionOrder.setId(incomingInspectionId);
        //进库验货单的采购单id
        incomingInspectionOrder.setPurchaseId(purchaseId);
        //进库验货单详情列表
        List<IncomingInspectionDetailsOrder> incomingInspectionDetailsOrders = new ArrayList<>();

        //全部重量
        double allWeight = 0.0;
        //该进库验货单的全部商品数量
        Integer num = 0;
        for(PurchaseOrderDetail purchaseOrderDetail:purchaseOrderDetailsDb){
            //当前进库验货单详情
            IncomingInspectionDetailsOrder incomingInspectionDetailsOrder = new IncomingInspectionDetailsOrder();
            //获取商品信息
            Product product = productService.getById(purchaseOrderDetail.getProductId());
            //获取库存信息,根据商品id
            QueryWrapper<StockOrder> sock_queryWrapper = new QueryWrapper<>();
            sock_queryWrapper.eq("product_id",purchaseOrderDetail.getProductId());
            StockOrder stockOrder = stockOrderService.getOne(sock_queryWrapper);
            //商品克重
            String gram = product.getGram();
            double weight = Double.parseDouble(gram);
            //计算总重量
            allWeight += weight;
            //计算商品数量
            num += Integer.parseInt(purchaseOrderDetail.getNumber());
            //添加进库验货详情的 进库验货id、商品id、商品数量、库位
            incomingInspectionDetailsOrder.setIncomingInspectionId(incomingInspectionId);
            incomingInspectionDetailsOrder.setGoodsId(purchaseOrderDetail.getProductId());
            incomingInspectionDetailsOrder.setGoodsNumber(purchaseOrderDetail.getNumber());
            incomingInspectionDetailsOrder.setWarehousePosition(stockOrder.getWarehousePosition());

            incomingInspectionDetailsOrders.add(incomingInspectionDetailsOrder);
        }
        //进库验货单详情信息插入 总重量、总数量、进库验货状态、生成时间
        incomingInspectionOrder.setTotalWeight(BigDecimal.valueOf(allWeight));
        incomingInspectionOrder.setTotalNumber(num.toString());
        incomingInspectionOrder.setInspectState(InspectStateEnum.NO_INSPECTION.getCode());
        incomingInspectionOrder.setInspectDate(new Date());
        //进库验货单插入到数据库
        save(incomingInspectionOrder);
        //进库验货详情单批量插入
        incomingInspectionDetailsOrderService.saveBatch(incomingInspectionDetailsOrders);

        PageDto<IncomingInspectionOrderDto> pageDtoList = getData(new PageDto<IncomingInspectionOrderDto>()).getData();
        ResponseDto<PageDto<IncomingInspectionOrderDto>> responseDto = getData(pageDtoList);
        responseDto.setMsg("添加成功！");

        return responseDto;
    }

    /**
     * 获取出库验证单数据
     * @param pageDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> getData(PageDto<IncomingInspectionOrderDto> pageDto) {
        //按条件 出库验货单id 搜索
        QueryWrapper<IncomingInspectionOrder> queryWrapper = new QueryWrapper<>();
        if("id".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据出库验货单id搜索
            queryWrapper.eq("id",pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //要返回的出库验货单信息
        List<IncomingInspectionOrder> list = list(queryWrapper);
        List<IncomingInspectionOrderDto> incomingInspectionOrderDtos = CopyUtil.copyList(list, IncomingInspectionOrderDto.class);
        //遍历出库验货单
        for (IncomingInspectionOrderDto incomingInspectionOrderDto : incomingInspectionOrderDtos) {
            //通过出库验货单id，查询到全部的出库验货单详情
            QueryWrapper<IncomingInspectionDetailsOrder> queryWrapper1 = new QueryWrapper<>();
            String id = incomingInspectionOrderDto.getId();
            queryWrapper1.eq("incoming_inspection_id",id);
            List<IncomingInspectionDetailsOrder> incomingInspectionDetailsOrders = incomingInspectionDetailsOrderService.list(queryWrapper1);
            List<IncomingInspectionDetailsOrderDto> incomingInspectionDetailsOrderDtos = CopyUtil.copyList(incomingInspectionDetailsOrders, IncomingInspectionDetailsOrderDto.class);
            //添加出库验货单详情
            incomingInspectionOrderDto.setIncomingInspectionDetailsOrderDtos(incomingInspectionDetailsOrderDtos);
        }

        PageInfo<IncomingInspectionOrder> pageInfo = new PageInfo<>(list);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(incomingInspectionOrderDtos);
        return ResponseDto.success(pageDto);
    }

    /**
     * 删除出库验证单
     * @param incomingInspectionOrderDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> delete(IncomingInspectionOrderDto incomingInspectionOrderDto) {
        IncomingInspectionOrder incomingInspectionOrder = CopyUtil.copy(incomingInspectionOrderDto, IncomingInspectionOrder.class);
        if(!removeById(incomingInspectionOrder)) {
            return ResponseDto.errorByMsg(CodeMsg.STOCK_DELETE_ERROR);
        }

        PageDto<IncomingInspectionOrderDto> pageDtoList = getData(new PageDto<IncomingInspectionOrderDto>()).getData();
        ResponseDto<PageDto<IncomingInspectionOrderDto>> responseDto = getData(pageDtoList);
        responseDto.setMsg("删除成功！");

        return responseDto;
    }

    /**
     * 修改验证状态
     * @param id
     * @param inspectState
     * @return
     */
    @Override
    public ResponseDto<PageDto<IncomingInspectionOrderDto>> updateInspectState(String id, Integer inspectState) {
        IncomingInspectionOrder incomingInspectionOrder = getById(id);
        if (incomingInspectionOrder==null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        incomingInspectionOrder.setInspectState(inspectState);
        updateById(incomingInspectionOrder);

        PageDto<IncomingInspectionOrderDto> pageDtoList = getData(new PageDto<IncomingInspectionOrderDto>()).getData();
        ResponseDto<PageDto<IncomingInspectionOrderDto>> responseDto = getData(pageDtoList);
        return responseDto;
    }

}
